#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

void huaping(Mat &image, Mat &frame)
{
    // converting image's color space (RGB) to grayscale
    Mat dst, abs_dst;
    Laplacian(image, dst, CV_16S, 3);
    convertScaleAbs(dst, abs_dst);
    Mat tmp_m, tmp_sd;
    double m = 0, sd = 0;
    //int threshold = 1000;
    meanStdDev(dst, tmp_m, tmp_sd);
    m = tmp_m.at<double>(0, 0);
    sd = tmp_sd.at<double>(0, 0);
    std::cout << "StdDev: " << sd * sd << std::endl; //æ–¹å·®

    stringstream meanValueStream;
    string meanValueString;
    meanValueStream << sd * sd;
    meanValueStream >> meanValueString;
    meanValueString = "huaping" + meanValueString;

    if ((sd * sd) > 1000)
    {
        printf("花屏\n");
        putText(frame, meanValueString, Point(20, 50), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(255, 255, 25), 2);
    }
}

int main() //huaping
{
    //???????????????VideoCapture??
    VideoCapture capture("/Users/lukeskywalker/Desktop/huaping.mov");
    //????????:??????isOpened??ture
    if (!capture.isOpened())
        cout << "fail to open!" << endl;
    //???????????????????
    bool stop = false;
    //????????
    Mat frame;
    Mat gray;
    Mat tempframe;
    int framenum = 0;

    while (!stop)
    {
        //?????
        if (!capture.isOpened())
        {
            cout << "??????" << endl;
            return -1;
        }

        capture >> frame;
        tempframe = frame;
        framenum++;

        if (framenum == 1)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY);
        }
        if (framenum >= 2)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY); //????????????currentFrame????tempFrame???

            //????
            //imshow("camera", tempframe);
            //imshow("moving area", gray);
        }

        if (!(framenum % 1))
        {
            printf("f:%d\n", framenum);
            //***suanfa;
        }

        huaping(gray, frame);
        imshow("Articulation", frame);
        //?????????????????
        waitKey(33);
    } //end while
}
